package com.example.test_activity.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 旭 on 2017/3/30/030.
 */

public class WeekUtil {

    GetSyllabus getSyllabus = new GetSyllabus();
    private List<Map<String, String>> list = new ArrayList<>();

    public List<Map<String, String>> Data(String html) {
        list.clear();
        list.addAll(getSyllabus.getWeek(html));

        for (Map<String, String> map : list) {
            for (String key : map.keySet()) {
                if (key.equals("weekend")) {
                    String temp = map.get(key);
                    temp = select(temp);
                    if (temp != null) {
                        map.put("weekend", temp);
                    }
                }
                if (key.equals("num")) {
                    String temp = map.get(key);
                    temp = select(temp);
                    if (temp != null) {
                        map.put("num", temp);
                    }
                }
                if (key.equals("week")) {
//                    Log.e("1", 1 + map.get(key) + 1);
                    if (select(map.get(key)) != null) {
                        map.put("week", select_week(map.get(key)));
                        map.put("max", max(map.get(key)));
                        map.put("min", min(map.get(key)));
                        map.put("flag", flag(map.get(key)));
                    }
                }
                if (key.equals("name")) {
                    map.put("name", select_name(map.get(key)));
                }
                if (key.equals("cno"))
                {
                    map.put("cno",map.get(key).substring(1,map.get(key).length()));
                }
                if (key.equals("school"))
                {
                    map.put("school",select_school(map.get(key)));
                }
                if (key.equals("one"))
                {
                    map.put("one",select_school(map.get(key)));
                }
                if (key.equals("two"))
                {
                    map.put("two",select_school(map.get(key)));
                }
            }
        }

        int i=0;
        for (Map<String,String> map : list)
        {
            for (String key : map.keySet())
            {
                if (key.equals("name"))
                {
                    map.put(key,map.get(key)+"_"+map.get("cno")+"("+map.get("school")+map.get("one")+map.get("two")+")");
                }
            }
            map.put("number",String.valueOf(i));
//            Log.e("信息",map.toString());
            ++i;
        }

        setWeek();

        return list;
    }

    private void setWeek()
    {
        for (Map<String, String> map : list) {
            if (!map.get("num").equals(" ")) {
                switch (Integer.valueOf(map.get("num"))) {
                    case 1:
                        map.put("num", "0");
                        break;
                    case 3:
                        map.put("num", "1");
                        break;
                    case 5:
                        map.put("num", "2");
                        break;
                    case 7:
                        map.put("num", "3");
                        break;
                    case 9:
                        map.put("num", "4");
                        break;
                    case 11:
                        map.put("num", "5");
                        break;
                }
            }
        }
    }

    private String select_school(String string)
    {
        String temp = null;
        int len = string.length();
        for (int i=0;i<len;++i)
        {
            if (string.charAt(i) != ' ' && string.charAt(i) != ' ')
            {
                temp = string.substring(i,len);
                break;
            }
        }
        return temp;
    }

    //获取课的时间
    private String select(String str) {
        String temp = null;
        int length = str.length();
        for (int i = 1; i < str.length(); ++i) {
            if (!(str.charAt(length - i) == ' ')) {
                temp = String.valueOf(str.charAt(length - i));
                break;
            }
        }
        return temp;
    }

    private String select_name(String str) {
        String temp = str.substring(1, str.length());

        return temp;
    }

    //获取周次
    private String select_week(String str) {
        int LENGTH = str.length();
        String temp = str.substring(2, LENGTH);
        return temp;
    }

    //最大周次
    private String max(String str) {
        String temp = null;
        int LENGTH = str.length();
        for (int i = str.indexOf('-') + 1; i < LENGTH; ++i) {
            if (str.charAt(i) == '周') {
                temp = str.substring(str.indexOf('-') + 1, i);
                break;
            }
        }
        return temp;
    }

    //最小周次
    private String min(String str) {
        String temp = str.substring(0, str.indexOf('-'));

        return temp;
    }

    //是否单双周
    private String flag(String str) {
        int min = str.length() - 3;
        int max = str.length() - 1;

        String temp = str.substring(min, max);

        if (temp.equals("单周")) {
            return "1";
        } else if (temp.equals("双周")) {
            return "0";
        } else {
            return "2";
        }
    }
}
